console.log('央视新闻脚本注入');


// 提取文章类新闻
function extractArticleNew(){
  let root = document.getElementById('main');
  const articleContent = root.querySelector('.article-content');
  const contents = {
    text: '',
    image: [],
    video: []
  };
  console.dir(document.images.length)
  Array.from(document.images).forEach((img, index) => {
    if (img.className.includes('adm-image-img')){
      console.dir(img)
    }
  })
  return;
  if (articleContent){
    const texts = Array.from(articleContent.querySelectorAll('p'));
    const newTexts = texts.reduce((text, currentDom) => {
      if (!currentDom.className.includes('image-desc') && currentDom.textContent.trim()){
        text += currentDom.textContent + '\n';
      }
      return text;
    }, '');
    console.log('新闻文本如下：');
    console.log(newTexts);
    contents.text = newTexts.trim();
    const imgs = articleContent.querySelectorAll('img.adm-image-img');
    if (imgs.length > 0){
      imgs.forEach(img => {
        console.dir(img)
        contents.image.push(img.currentSrc);
      });
      console.log('新闻图片如下： ');
      console.log(contents.image);
    }
    return contents;
  } else {
    return false;
  }
}

function handleNew(){
  console.log(window.location)
  // 文章类新闻
  let articleReg = /^\/article/i;
  if (articleReg.test(window.location.pathname)){
    return extractArticleNew();
  } else {
    return false
  }
}

chrome.runtime.onMessage.addListener((message, sender, sendMessage) => {
  console.log(message);
  if (message.id === 'newDetail') {
    const result = handleNew();
    sendMessage(result);
  }
});